/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "NRF24L01.h"
#include "stdbool.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

uint8_t key_status[4]= {0,0,0,0};
uint8_t sw_left_status[3]= {0,0,0};
uint8_t sw_right_status[3]= {0,0,0};
uint32_t adcBuf[6]= {0};
uint8_t sw_status;
char rx_buff[32]= {0};

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
void MOTO1_A(uint8_t flag,int speed);
void MOTO1_B(uint8_t flag,int speed);
void MOTO2_A(uint8_t flag,int speed);
void MOTO2_B(uint8_t flag,int speed);
void stop(void);
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */


#define PWM_SW_1_ON __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, 2500);
#define PWM_SW_1_OFF __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, 0);

#define PWM_SW_2_ON __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_2, 3300);
#define PWM_SW_2_OFF __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_2, 0);

#define PWM_SW_3_ON __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_3, 3300);
#define PWM_SW_3_OFF __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_3, 0);

#define SW_1_ON HAL_GPIO_WritePin(SW1_GPIO_Port,SW1_Pin,GPIO_PIN_SET);
#define SW_1_OFF HAL_GPIO_WritePin(SW1_GPIO_Port,SW1_Pin,GPIO_PIN_RESET);

#define SW_2_ON HAL_GPIO_WritePin(SW2_GPIO_Port,SW2_Pin,GPIO_PIN_SET);
#define SW_2_OFF HAL_GPIO_WritePin(SW2_GPIO_Port,SW2_Pin,GPIO_PIN_RESET);

#define SW_3_ON HAL_GPIO_WritePin(SW3_GPIO_Port,SW3_Pin,GPIO_PIN_SET);
#define SW_3_OFF HAL_GPIO_WritePin(SW3_GPIO_Port,SW3_Pin,GPIO_PIN_RESET);

#define SW_4_ON HAL_GPIO_WritePin(SW4_GPIO_Port,SW4_Pin,GPIO_PIN_SET);
#define SW_4_OFF HAL_GPIO_WritePin(SW4_GPIO_Port,SW4_Pin,GPIO_PIN_RESET);

//#define MODE_HE
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//输入值分别是：输入值，输入最小值，输入最大值，输出最小值，输出最大值
//输入值分别是：输入值，输入最小值，输入最大值，输出最小值，输出最大值
float map(float val, float I_Min, float I_Max, float O_Min, float O_Max)
	{
		return(((val-I_Min)*((O_Max-O_Min)/(I_Max-I_Min)))+O_Min);
   }
  

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
    /* USER CODE BEGIN 1 */

    /* USER CODE END 1 */

    /* MCU Configuration--------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* USER CODE BEGIN Init */

    /* USER CODE END Init */

    /* Configure the system clock */
    SystemClock_Config();

    /* USER CODE BEGIN SysInit */

    /* USER CODE END SysInit */

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_TIM1_Init();
    MX_TIM2_Init();
    MX_TIM3_Init();
    /* USER CODE BEGIN 2 */

    //开启定时器
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);
    HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);
    HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
    HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_4);
    HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_3);
    HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_4);
    
    #ifndef MODE_HE
    __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, 1000);
    #endif



    Init_NRF24L01();

    if(NRF24L01_Check())
    {
        while(1) {
            HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);
            HAL_Delay(40);
        }
    }

    //开启接收模式
    RX_Mode();
    PWM_SW_1_OFF
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */


    while (1)
    {
        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */

        if(NRF24L01_RxPacket((uint8_t *)rx_buff)==0) {
            // 解析接收到的数据
            int success_num = sscanf(rx_buff, "%04u,%04u,%04u,%04u,%04u,%04u,%c",&adcBuf[0], &adcBuf[1], &adcBuf[2], &adcBuf[3], &adcBuf[4], &adcBuf[5], &sw_status);
            if(success_num == 7) {
#ifdef MODE_HE
             //开关1
                if((sw_status & (1 << 0))) {
                    PWM_SW_1_ON
                }
                else {
                    PWM_SW_1_OFF
                }
                
                //右边 三段开关  上  中  下
                if ((sw_status & (1 << 6)) && !(sw_status & (1 << 7))) {
                    
                   

                    PWM_SW_2_ON
                    PWM_SW_3_ON
                    MOTO2_A(1,adcBuf[4]/3);
                    MOTO2_B(1,adcBuf[5]/3);
                    //左边 三段开关  上  中  下
                    if ((sw_status & (1 << 4)) && !(sw_status & (1 << 5))) {
                        MOTO1_A(1,adcBuf[1]/3);
                        MOTO1_B(1,adcBuf[1]/3);
                    }
                    else if (!(sw_status & (1 << 4)) && (sw_status & (1 << 5))) {
                        stop();
                    }
                    else {
                        MOTO1_A(0,adcBuf[1]/3);
                        MOTO1_B(0,adcBuf[1]/3);
                    }
                }
                else if (!(sw_status & (1 << 6)) && (sw_status & (1 << 7))) {//紧急停止
                    stop();
                }
                else {//紧急停止
                    stop();
                }
#else
                //开关4
                if((sw_status & (1 << 3))) { //开机
                    uint32_t fire_value = map(adcBuf[4],0,3300,1000,2000);
                    __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, fire_value); //油门
                    /*******左轮********/
                    if(adcBuf[1]>1700) {        //左转
                        MOTO1_A(1,1000);
                    } else if(adcBuf[1]<1500) { //右转
                        MOTO1_A(0,1000);
                    } else {                    //空
                        MOTO1_A(1,0);
                    }
                    /*******右轮********/
                    if(adcBuf[3]>1700) {        //左转
                        MOTO1_B(1,1000);
                    } else if(adcBuf[3]<1500) { //右转
                        MOTO1_B(0,1000);
                    } else {                    //空
                        MOTO1_B(1,0);
                    }
                    /******前进后退*********/
                    if(adcBuf[0]>1700) {        //前进
                        MOTO2_A(1,1000);
                    } else if(adcBuf[0]<1500) { //后退
                        MOTO2_A(0,1000);
                    } else {                    //空
                        MOTO2_A(1,0);
                    }
                    /******上升下降********/
                    if(adcBuf[2]>1700) {        //升高
                        MOTO2_B(1,1000);
                    } else if(adcBuf[2]<1500) { //下降
                        MOTO2_B(0,1000);
                    } else {                    //空
                        MOTO2_B(1,0);
                    }
                }
                else { //关机
                    __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, 1000);
                    __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_2, 1000);
                    __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_3, 1000);
                    __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_4, 1000);
                    __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 1000);
                    __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 1000);
                    __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3, 1000);
                    __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4, 1000);
                }
#endif

//                //开关1
//                if((sw_status & (1 << 0))) {
//                    MOTO1_A(1);
//                }
//                else {
//                    MOTO1_A(0);
//                }
//                //开关2
//                if((sw_status & (1 << 1))) {
//                    MOTO1_B(1);
//                }
//                else {
//                    MOTO1_B(0);
//                }
//                //开关3
//                if((sw_status & (1 << 2))) {
//                    MOTO2_A(1);
//                }
//                else {
//                    MOTO2_A(0);
//                }
//                //开关4
//                if((sw_status & (1 << 3))) {
//                    MOTO2_B(1);
//                }
//                else {
//                    MOTO2_B(0);
//                }

                //左边 三段开关  上  中  下
//                if ((sw_status & (1 << 4)) && !(sw_status & (1 << 5))) {
//                    SW_1_ON
//                    SW_2_OFF
//                    SW_3_OFF
//                }
//                else if (!(sw_status & (1 << 4)) && (sw_status & (1 << 5))) {
//                    SW_1_OFF
//                    SW_2_ON
//                    SW_3_OFF
//                }
//                else {
//                    SW_1_OFF
//                    SW_2_OFF
//                    SW_3_ON
//                }

                //右边 三段开关  上  中  下
//                if ((sw_status & (1 << 6)) && !(sw_status & (1 << 7))) {
//                    SW_1_ON
//                    SW_2_OFF
//                    SW_3_OFF
//                }
//                else if (!(sw_status & (1 << 6)) && (sw_status & (1 << 7))) {
//                    SW_1_OFF
//                    SW_2_ON
//                    SW_3_OFF
//                }
//                else {
//                    SW_1_OFF
//                    SW_2_OFF
//                    SW_3_ON
//                }

            }
            HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);
        }
//        HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);
//        HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
//        HAL_Delay(100);

    }
    /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    /** Initializes the RCC Oscillators according to the specified parameters
    * in the RCC_OscInitTypeDef structure.
    */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
        Error_Handler();
    }

    /** Initializes the CPU, AHB and APB buses clocks
    */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                                  |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
    {
        Error_Handler();
    }

    /** Enables the Clock Security System
    */
    HAL_RCC_EnableCSS();
}

/* USER CODE BEGIN 4 */
//电机1控制 1->正  2->反  speed取值 0-1000
void MOTO1_A(uint8_t flag,int speed) {
    if(speed<0) {
        speed=0;
    }
    else if(speed>1000) {
        speed = 1000;
    }
    if(flag) {
        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3, 0);
        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4, speed);
    } else {
        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3, speed);
        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4, 0);
    }
}
void MOTO1_B(uint8_t flag,int speed) {
    if(speed<0) {
        speed=0;
    }
    else if(speed>1000) {
        speed = 1000;
    }
    if(flag) {
        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 0);
        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, speed);
    } else {
        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, speed);
        __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 0);
    }
}
void MOTO2_A(uint8_t flag,int speed) {
    if(speed<0) {
        speed=0;
    }
    else if(speed>1000) {
        speed = 1000;
    }
    if(flag) {
        __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_3, 0);
        __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_4, speed);
    } else {
        __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_3, speed);
        __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_4, 0);
    }
}
void MOTO2_B(uint8_t flag,int speed) {
    if(speed<0) {
        speed=0;
    }
    else if(speed>1000) {
        speed = 1000;
    }
    if(flag) {
        __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, 0);
        __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_2, speed);
    } else {
        __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, speed);
        __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_2, 0);
    }
}
void stop(void) {
    //双路驱动 刹车
    __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, 1000);
    __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_2, 1000);
    __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_3, 1000);
    __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_4, 1000);
    __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, 1000);
    __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, 1000);
    __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_3, 1000);
    __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_4, 1000);

    //单向PWM开关 关
    //PWM_SW_1_OFF
    PWM_SW_2_OFF
    PWM_SW_3_OFF

    //开关 关
    SW_1_OFF
    SW_2_OFF
    SW_3_OFF
    SW_4_OFF
}
//                //开关1
//                if((sw_status & (1 << 0))) {
//                    MOTO1_A(1);
//                }
//                else {
//                    MOTO1_A(0);
//                }
//                //开关2
//                if((sw_status & (1 << 1))) {
//                    MOTO1_B(1);
//                }
//                else {
//                    MOTO1_B(0);
//                }
//                //开关3
//                if((sw_status & (1 << 2))) {
//                    MOTO2_A(1);
//                }
//                else {
//                    MOTO2_A(0);
//                }
//                //开关4
//                if((sw_status & (1 << 3))) {
//                    MOTO2_B(1);
//                }
//                else {
//                    MOTO2_B(0);
//                }

//左边 三段开关  上  中  下
//                if ((sw_status & (1 << 4)) && !(sw_status & (1 << 5))) {
//                    SW_1_ON
//                    SW_2_OFF
//                    SW_3_OFF
//                }
//                else if (!(sw_status & (1 << 4)) && (sw_status & (1 << 5))) {
//                    SW_1_OFF
//                    SW_2_ON
//                    SW_3_OFF
//                }
//                else {
//                    SW_1_OFF
//                    SW_2_OFF
//                    SW_3_ON
//                }

//右边 三段开关  上  中  下
//                if ((sw_status & (1 << 6)) && !(sw_status & (1 << 7))) {
//                    SW_1_ON
//                    SW_2_OFF
//                    SW_3_OFF
//                }
//                else if (!(sw_status & (1 << 6)) && (sw_status & (1 << 7))) {
//                    SW_1_OFF
//                    SW_2_ON
//                    SW_3_OFF
//                }
//                else {
//                    SW_1_OFF
//                    SW_2_OFF
//                    SW_3_ON
//                }

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while (1)
    {
        HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_0);
        HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);
        HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_7);
        HAL_Delay(50);
    }
    /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
